package io.renren.modules.ht.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.ht.entity.ContractsAmountEntity;
import io.renren.modules.ht.entity.ContractsEntity;
import io.renren.modules.ht.entity.ContractsRelEntity;
import io.renren.modules.ht.service.ContractsAmountService;
import io.renren.modules.ht.service.ContractsRelService;
import io.renren.modules.ht.service.ContractsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author chenhonghua
 * @email 277765406@qq.com
 * @date 2023-11-20 13:40:14
 */
@RestController
@RequestMapping("ht/contracts")
public class ContractsController {
    @Autowired
    private ContractsService contractsService;
    @Autowired
    private ContractsRelService contractsRelService;
    @Autowired
    private ContractsAmountService contractsAmountService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ht:contracts:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = contractsService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{contractsId}")
    @RequiresPermissions("ht:contracts:info")
    public R info(@PathVariable("contractsId") Integer contractsId){
        ContractsEntity contracts = contractsService.getById(contractsId);
        Map<String, Object> params =  new HashMap<>();
        params.put("contractId",contracts.getContractId());
        params.put("page","1");
        params.put("limit","1");
        PageUtils page = contractsRelService.queryPage(params);
        if(page.getTotalCount()>0){
            List list = page.getList();
            ContractsRelEntity contractsRel = (ContractsRelEntity)list.get(0);
            contracts.setRoomId(contractsRel.getRoomId());
            contracts.setCustomerId(contractsRel.getCustomerId());
        }
        params.put("page","1");
        params.put("limit","1");
        page = contractsAmountService.queryPage(params);
        if(page.getTotalCount()>0){
            List list = page.getList();
            ContractsAmountEntity contractsAmount = (ContractsAmountEntity)list.get(0);
            //contracts.setRoomId(contractsAmount.getRoomId());
            contracts.setAmount(contractsAmount.getAmount());
        }
        return R.ok().put("contracts", contracts);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ht:contracts:save")
    public R save(@RequestBody ContractsEntity contracts){
		boolean flag = contractsService.save(contracts);
        if(flag){
            ContractsRelEntity contractsRel = new ContractsRelEntity();
            contractsRel.setContractId(contracts.getContractId());
            contractsRel.setCustomerId(contracts.getCustomerId());
            contractsRel.setRoomId(contracts.getRoomId());
            contractsRelService.save(contractsRel);
            ContractsAmountEntity contractsAmount = new ContractsAmountEntity();
            contractsAmount.setContractId(contracts.getContractId());
            contractsAmount.setAmount(contracts.getAmount());
            contractsAmount.setRoomId(contracts.getRoomId());
            contractsAmountService.save(contractsAmount);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ht:contracts:update")
    public R update(@RequestBody ContractsEntity contracts){
        boolean flag = contractsService.updateById(contracts);
        if(flag){
            Map<String, Object> params =  new HashMap<>();
            params.put("contractId",contracts.getContractId());
            params.put("page","1");
            params.put("limit","1");
            PageUtils page = contractsRelService.queryPage(params);
            if(page.getTotalCount()>0){
                List list = page.getList();
                ContractsRelEntity contractsRel = (ContractsRelEntity)list.get(0);
                contractsRel.setRoomId(contracts.getRoomId());
                contractsRel.setCustomerId(contracts.getCustomerId());
                contractsRelService.updateById(contractsRel);
            }else{
                ContractsRelEntity contractsRel = new ContractsRelEntity();
                contractsRel.setContractId(contracts.getContractId());
                contractsRel.setCustomerId(contracts.getCustomerId());
                contractsRel.setRoomId(contracts.getRoomId());
                contractsRelService.save(contractsRel);
            }
            params.put("page","1");
            params.put("limit","1");
            page = contractsAmountService.queryPage(params);
            if(page.getTotalCount()>0){
                List list = page.getList();
                ContractsAmountEntity contractsAmount = (ContractsAmountEntity)list.get(0);
                contractsAmount.setRoomId(contracts.getRoomId());
                contractsAmount.setAmount(contracts.getAmount());
                contractsAmountService.updateById(contractsAmount);
            }else{
                ContractsAmountEntity contractsAmount = new ContractsAmountEntity();
                contractsAmount.setContractId(contracts.getContractId());
                contractsAmount.setAmount(contracts.getAmount());
                contractsAmount.setRoomId(contracts.getRoomId());
                contractsAmountService.save(contractsAmount);
            }
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ht:contracts:delete")
    public R delete(@RequestBody Integer[] contractsIds){
		contractsService.removeByIds(Arrays.asList(contractsIds));
        return R.ok();
    }
    @RequestMapping("/getContractsList")
    public R getContractsList(){
        List<ContractsEntity> list = contractsService.list();
        return R.ok().put("data", list);
    }
}
